<template>
  <view>
    <!-- 使用自定义的搜索文件 -->
    <my-search @click="gotoSearch"></my-search>
    <view class="cate-scroll-container">
      <scroll-view scroll-y="true" :style="{height:wh + 'px'}" class="left-scroll">
        <block  v-for="(item1,i1) in cateList" :key="i1">
          <view :class="['left-scroll-item', i1=== active? 'active' : '']" @click="activeSelect(i1)">{{item1.cat_name}}</view>
        </block>
      </scroll-view>
      <scroll-view scroll-y="true" :scroll-top="scrollTop" :style="{height:wh + 'px'}" class="right-scroll">
        <view class="cate-title" v-for="(item2,i2) in cateList2" :key="i2">
          <view>{{item2.cat_name}}</view>
          <view class="cate-box">
          <view class="cate-item" v-for="(item3,i3) in item2.children" :key="i3" @click="gotoGoodsList(item3)">
            <image :src="item3.cat_icon" mode=""></image>
            <text>{{item3.cat_name}}</text>
            </view>
          </view>
          </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        wh: 0,
        cateList:[],
        cateList2: [],
        active: 0,
        scrollTop: 0
      }
    },
    onLoad () {
      const deviceInfo = uni.getSystemInfoSync()
      console.log('deviceInfo',deviceInfo)
      this.wh = deviceInfo.windowHeight
      this.getCateList()
    },
    methods:{
    async getCateList () {
       const {data:res} = await uni.$http.get('/api/public/v1/categories')
       console.log('分类列表',res)
       if(res.meta.status !==200) return uni.$showMag()
       this.cateList = res.message
       this.cateList2 = res.message[0].children
      },
      activeSelect (key) {
        this.active = key
        this.cateList2 = this.cateList[key].children
        this.scrollTop = this.scrollTop ===0 ? 1:0
      },
      gotoGoodsList (item) {
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid='+ item.cat_id
        })
      },
      gotoSearch () {
         console.log('OK')
         uni.navigateTo({
           url:"/subpkg/search/search"
         })
      }
    }
  }
</script>

<style lang="scss">
.cate-scroll-container {
  display:flex;
  
  .left-scroll {
    width: 120px;
    .left-scroll-item {
      background-color: #f7f7f7;
      line-height: 60px;
      text-align: center;
      font-size: 12px;
      position: relative;
      &.active {
        background-color: #ffffff;
      &::before {
        content:'';
        display: block;
        width: 3px;
        height: 30px;
        background-color: #c00000
        ;
        position: absolute;
        left: 0px;
        top: 50%;
        transform: translateY(-50%);
      }
      }
    }
  }
  .right-scroll {
    width: 100%;
    background-color: #ffffff;
    .cate-title {
      font-size: 12px;
      font-weight: bold;
      text-align: center;
      padding: 15px 0px;
      
      .cate-box {
        display: flex;
        flex-wrap: wrap;
        .cate-item {
          width: 33.33%;
          display: flex;
          flex-direction: column;
          align-items: center;
          
          image {
            width: 60px;
            height: 60px;
          }
          
          text {
            font-size: 12px;
          }
        }
      }
    }
  }
}


</style>
